package Controllers;

import Bean.BeanClass;
import Impl.ClassServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ClassAddController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    ClassServiceImpl classService;
	 @RequestMapping(value = "/ClassAdd")
		@ResponseBody
		public JSONObject userRegister(HttpServletRequest request,@RequestBody JSONObject in) throws Exception {
		 	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
	        classService=(ClassServiceImpl) applicationContext.getBean("ClassServiceImpl");
			JSONObject obj = new JSONObject();
			BeanClass aClass = new BeanClass();
			aClass.setClass_name(in.getString("classname"));
		 	aClass.setClass_url(in.getString("classurl"));
		 	aClass.setClass_price(Double.parseDouble(in.getString("classprice")));
		 	aClass.setType(in.getInteger("classtype"));
		 	aClass.setNum(in.getInteger("classnum"));
		 	aClass.setPlace_id(in.getInteger("classplace"));
		 	aClass.setTeacher_id(in.getString("classteacher"));
		 	System.out.println("name:"+aClass.getClass_name());
		 	classService.classAdd(aClass.getClass_name(), aClass.getClass_url(),aClass.getClass_price(), aClass.getType(), aClass.getNum(),aClass.getTeacher_id(),aClass.getPlace_id());
			obj.put("status", "OK");
			return obj;
		}
}
